package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.User;
import service.UserService;
import util.Page;

import java.util.List;

@Controller
@RequestMapping("/user")

// /user/list
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/tologin")
    public String tologin(){
        return "login";
    }

    @RequestMapping("/login")
    public String login(User user){
        boolean flag = userService.login(user);
        if(flag){
            return "index";
        }else{
            return "login";
        }

    }

    @RequestMapping("/toregist")
    public String toregist(){
        return "regist";
    }

    @RequestMapping("/regist")
    @ResponseBody
    public String regist(User user) {
        int result = userService.addUser(user);
        if (result>0) {
            return "true";
        } else {
            return "false";
        }
    }

    @RequestMapping("/list/{currentPage}")
    public String findAll(User user,@PathVariable("currentPage") int currentPage,Model model) {
        Page page = new Page();
        page.setCurrentPage(currentPage);
        // 查询总记录数
        int totalCount = userService.count(user);
        page.setTotalCount(totalCount);
        page.setTotalPage();
        // 计算偏移量
        int offset = (currentPage - 1) * page.getPageSize();
        List<User> users = userService.findAll(user, offset, page.getPageSize());
        model.addAttribute("users", users);
        model.addAttribute("page", page);
        model.addAttribute("user", user);

        return "user/list";
    }

    @RequestMapping("/toadd")
    public String toAdd() {
        return "user/add";
    }

    @RequestMapping("/addSave")
    public String addUser(User user) {
       int result = userService.addUser(user);
       if (result>0) {
           return "redirect:/user/list";
       } else {
           return "error";
       }
    }

    @RequestMapping("/delete/{id}")
    public String deleteUser(@PathVariable("id") int id) {
        int result = userService.deleteUser(id);
        if (result>0) {
            return "redirect:/user/list";
        } else {
            return "error";
        }
    }

    @RequestMapping("/updateSave")
    public String updateUser(User user) {
        int result = userService.updateUser(user);
        if (result>0) {
            return "redirect:/user/list";
        } else {
            return "error";
        }
    }

    @RequestMapping("/toupdate/{id}")
    public String toUpdate(@PathVariable("id") int id,Model model) {
        User user = userService.findUserById(id);
        model.addAttribute("user", user);
        return "user/update";
    }

//    @RequestMapping("/checkUserNameUnique")
//    @ResponseBody
//    public String checkUserNameUnique(@RequestParam("name") String name){
//    	User user = userService.findUserByUserName(name);
//    	if(user==null){
//    		return "true";
//    	}else{
//    		return "false";
//    	}
//    }

    @RequestMapping("/checkUserNameUnique")
    @ResponseBody
    public String checkUserNameUnique(User user){
        User u = userService.findUserByUserName(user.getName());
        if(u==null){
            return "true";
        }else{
            return "false";
        }
    }
}
